package servlet;

import dao.OrderDAO;
import pojo.Order;
import utils.ApiResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;

@WebServlet("/order")
public class OrderServlet extends BaseServlet {

    // 添加订单
    public void addOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int userId = Integer.parseInt(req.getParameter("userId"));
        LocalDateTime orderDate = LocalDateTime.now();
        BigDecimal totalAmount = new BigDecimal(req.getParameter("totalAmount"));
        String paymentStatus = req.getParameter("paymentStatus");
        String shippingAddress = req.getParameter("shippingAddress");

        Order order = new Order();
        order.setUserId(userId);
        order.setOrderDate(orderDate);
        order.setTotalAmount(totalAmount);
        order.setPaymentStatus(paymentStatus);
        order.setShippingAddress(shippingAddress);

        try {
            OrderDAO.addOrder(order);
            sendJsonResponse(resp, new ApiResult(200, "添加订单成功", null));
        } catch (SQLException e) {
            sendJsonResponse(resp, new ApiResult(500, "添加订单失败", e.getMessage()));
        }
    }

    // 更新订单信息
    public void updateOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int orderId = Integer.parseInt(req.getParameter("orderId"));
        int userId = Integer.parseInt(req.getParameter("userId"));
        LocalDateTime orderDate = LocalDateTime.parse(req.getParameter("orderDate")); // Assuming date format can be parsed directly
        BigDecimal totalAmount = new BigDecimal(req.getParameter("totalAmount"));
        String paymentStatus = req.getParameter("paymentStatus");
        String shippingAddress = req.getParameter("shippingAddress");

        Order order = new Order();
        order.setOrderId(orderId);
        order.setUserId(userId);
        order.setOrderDate(orderDate);
        order.setTotalAmount(totalAmount);
        order.setPaymentStatus(paymentStatus);
        order.setShippingAddress(shippingAddress);

        try {
            OrderDAO.updateOrder(order);
            sendJsonResponse(resp, new ApiResult(200, "更新订单信息成功", null));
        } catch (SQLException e) {
            sendJsonResponse(resp, new ApiResult(500, "更新订单信息失败", e.getMessage()));
        }
    }

    // 删除订单
    public void deleteOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int orderId = Integer.parseInt(req.getParameter("orderId"));

        try {
            OrderDAO.deleteOrder(orderId);
            sendJsonResponse(resp, new ApiResult(200, "删除订单成功", null));
        } catch (SQLException e) {
            sendJsonResponse(resp, new ApiResult(500, "删除订单失败", e.getMessage()));
        }
    }

    // 获取所有订单
    public void getAllOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            List<Order> orders = OrderDAO.getAllOrders();
            sendJsonResponse(resp, new ApiResult(200, "获取所有订单成功", orders));
        } catch (SQLException e) {
            sendJsonResponse(resp, new ApiResult(500, "获取所有订单失败", e.getMessage()));
        }
    }

}
